﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Domain.Contracts;
using Microsoft.Practices.ServiceLocation;
using App.Domain.Handlers;
using App.Web.Models;

namespace App.Web.Controllers {
    public class CompanyController : AuthorizedController {

        public CompanyController(IUserServices userServices,
            IServiceLocator serviceLocator)
            : base(userServices, serviceLocator) {
        }

        [Authorize]
        public ActionResult Index() {

            var models = new ComanyIndexViewModel() { 
            
                JobPositionList = Using<GetExpiringPosition>().Execute(this.CurrentUser.CompanyId).Take(5),
                ApplicationList = Using<GetRecentPosition>().Execute(this.CurrentUser.CompanyId).Take(5),
                BulletinList = Using<GetBulletinPublished>().Execute().Take(5),
            
            
            };

            return View(models);
        }

        //Get
        public ActionResult Details(Guid id) {
            var model = Using<GetCompany>()
                .Execute(id);
            return View(model);
        }

        public ActionResult Create() {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CompanyFormModel companyForm) {
            if (ModelState.IsValid) {

                companyForm.DateCreated = DateTime.Now;
                companyForm.LastUpdated = DateTime.Now;
                companyForm.Id = Guid.NewGuid();


                Using<CreateCompany>()
                    .Execute(companyForm);
            }

            return View(companyForm);
        }

        public ActionResult Edit(Guid Id) {
            //var com = Using<GetCompany>()
            //    .Execute(Id);

            //var model = new CompanyFormModel {
            //    CompanyName = com.CompanyName,
            //    DateCreated = com.DateCreated,
            //    Id = com.Id
            //};

            //return View(model);

            return View();
        }

        [HttpPost]
        public ActionResult Edit(CompanyFormModel companyForm) {

            if (ModelState.IsValid) {
                companyForm.LastUpdated = DateTime.Now;

                Using<UpdateCompany>()
                    .Execute(companyForm);
            }

            return View();
        }

        [HttpPost]
        public ActionResult Delete(Guid id) {
            Using<DeleteCompany>().Execute(id);
            return RedirectToRoute("index");
        }

    }
}
